﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace SalomonWebsite
{
    public partial class Default : System.Web.UI.Page
    {
        protected string strCon = Global.Connection;
        public string ImageWeb = Global.ImageWeb;
        public int countRow = 0;
        public int countPage = 0;
        public string strCommCtrlItem = "";
        public string strCss = "";

        public string HotLine;
        public string SiteKeywords;
        public string SiteDescription;
        public string SiteName;


        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                DataTable dtConfig = Bll.ConfigInfo.MULTIPLE(Global.Connection);
                if (dtConfig.Rows.Count == 1)
                {
                    SiteName = dtConfig.Rows[0]["SiteName"].ToString();
                    SiteKeywords = dtConfig.Rows[0]["SiteKeywords"].ToString();
                    SiteDescription = dtConfig.Rows[0]["SiteDescription"].ToString();
                    HotLine = dtConfig.Rows[0]["HotLine"].ToString();

                }
                dtConfig.Dispose();

                GetAdList(strCon);
            }
        }

        //protected DataTable CreateTable()
        //{
        //    DataTable dt = new DataTable();
        //    dt.Columns.Add("Num", typeof(Int32));
        //    return dt;
        //}

        protected void GetAdList(string strCon)
        {
            DataTable dt = Bll.CategoryAd.MULTIPLE(strCon,3);

            if (dt.Rows.Count > 0)
            {
                string tmpDate = DateTime.Now.ToString();

                DataRow[] drs = dt.Select(" '" + tmpDate + "' >= [StartDate] AND '" + tmpDate + "' <= [EndDate]");
                if (drs.Length > 0)
                {
                    DataTable dtNum = dt.Clone();
                    dtNum.Columns.Add("LiStart", typeof(String));
                    dtNum.Columns.Add("LiEnd", typeof(String));
                    for (int i = 0; i < drs.Length; i++)
                    {
                        DataRow newRow = dtNum.NewRow();

                        newRow["PKID"] = Convert.ToInt32(drs[i]["PKID"]);
                        if ((i + 1) % 6 == 1)
                            newRow["LiStart"] = "<li>";
                        if ((i + 1) % 6 == 0 || (i + 1) == drs.Length)
                            newRow["LiEnd"] = "</li>";
                        newRow["AdTitle"] = drs[i]["AdTitle"].ToString();
                        newRow["Keywords"] = drs[i]["Keywords"].ToString();
                        newRow["ImgURL"] = drs[i]["ImgURL"].ToString();
                        newRow["Description"] = drs[i]["Description"].ToString();
                        dtNum.Rows.Add(newRow);
                    }
                    if (dtNum.Rows.Count > 0)
                    {
                        this.repLi.DataSource = dtNum.DefaultView;
                        this.repLi.DataBind();
                    }
                    dtNum.Dispose();

                    for (int i = 0; i < (drs.Length / 6) + 1; i++)
                    {
                        strCommCtrlItem += "<a href=\"javascript:void(0);\" class=\"CommCtrlItem\"><span></span></a>";
                    }

                    if (drs.Length < 4)
                    {
                        strCss = "half";
                    }

                }
                else
                {
                    this.CommGroup.Visible = false;
                    this.CommCtrl.Visible = false;
                }
            
            }
            dt.Dispose();

            
        }

        //protected void repLi_ItemDataBound(object sender, RepeaterItemEventArgs e)
        //{
        //    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        //    {
        //        Repeater repAd = (Repeater)e.Item.FindControl("repAdList");
        //        DataRowView drv = (DataRowView)e.Item.DataItem;
        //        int id = Convert.ToInt32(drv["Num"]);
        //        repAd.DataSource = GetAd(strCon, 6, id, out countRow, out countPage);
        //        repAd.DataBind();
        //        repAd.Dispose();
        //    }
        //}

        //protected DataTable GetAd(string strCon, int size, int index, out int countRow, out int countPage)
        //{
        //    DataTable dt = new DataTable();
        //    dt = Bll.CategoryAd.MULTIPLE(strCon, size, index, out countRow, out countPage);
        //    return dt;
        //}
    }
}
